package com.djsearch.data;


import org.json.JSONArray;
import org.json.JSONException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import com.djsearch.bean.LvYouCityRequestParmeterBean;
import com.djsearch.bean.LvYouGoodsRequestPararmeterBean;
import com.djsearch.bean.LvYouRequestParmeterBaseBean;
/**
 * 旅游数据请求及获取返回数据后 ，提取json封装类
 * @author chenyang2
 * @version 1.0
 */
public class LvYouDataRequest {
	private static LvYouDataRequest lvYouDataRequest = null;
	private static String wcfUrl = "http://www.dianjinwang.net/webApi/wapinoajax.svc";
	private static String nameSpaceUrl = "http://www.dianjinwang.net/";
	private static String actionUrl = "http://www.dianjinwang.net/CalculatorService/";
	private JSONArray jsonArray;
	private SoapObject object;
	
	public static LvYouDataRequest getIntence() {
		if (lvYouDataRequest == null) {
			lvYouDataRequest = new LvYouDataRequest();
		}
		return lvYouDataRequest;
		
	}
	
	/**
	 * 设置请求的参数
	 */
	public void setObjectProperty (SoapObject object, LvYouRequestParmeterBaseBean baseBean) {
		if (baseBean instanceof LvYouCityRequestParmeterBean) {
			LvYouCityRequestParmeterBean bean = (LvYouCityRequestParmeterBean) baseBean;
			object.addProperty(bean.getByOneNum(),
					bean.getByOneNumValue());
		} else if (baseBean instanceof LvYouGoodsRequestPararmeterBean) {
			LvYouGoodsRequestPararmeterBean bean = (LvYouGoodsRequestPararmeterBean) baseBean;
			object.addProperty("input",bean.getInput());
			object.addProperty("MoneyNum",bean.getMoneyNum());
			object.addProperty("XingNum",bean.getXingNum());
			object.addProperty("ByOneNum",bean.getByOneNum());
			object.addProperty("ByTwoNum",bean.getByTwoNum());
			object.addProperty("classNum",bean.getClassNum());
			object.addProperty("zhutiId",bean.getZhutiId());
			object.addProperty("dayId",bean.getDayId());
			object.addProperty("Start",bean.getStart());
			object.addProperty("EndNum",bean.getEndNum());
		}
	}
	
	/**
	 * 请求方法入口
	 * @param methodName
	 * @param baseBean
	 * @return
	 * [{"id":2,"Sale_ClassTop":1,"Sale_ClassName":"周边游","Sale_ClassPath":"1","Sale_ClassDepth":2},
	 * {"id":3,"Sale_ClassTop":1,"Sale_ClassName":"省内游","Sale_ClassPath":"1","Sale_ClassDepth":2},
	 * {"id":4,"Sale_ClassTop":1,"Sale_ClassName":"国内游","Sale_ClassPath":"1","Sale_ClassDepth":2},
	 * {"id":5,"Sale_ClassTop":1,"Sale_ClassName":"国外游","Sale_ClassPath":"1","Sale_ClassDepth":2},
	 * {"id":6,"Sale_ClassTop":1,
	 * "Sale_ClassName":"商品兑换","Sale_ClassPath":"1","Sale_ClassDepth":2}]; }
	 */
	public int requestWcf(String methodName,LvYouRequestParmeterBaseBean baseBean) {
		HttpTransportSE httpTransportSE = new HttpTransportSE(wcfUrl);//URL_STRING是WCF地址 
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);//这里的版本是1.1的  
		object = new SoapObject(nameSpaceUrl, methodName); 
		if (baseBean != null) {
			setObjectProperty(object,baseBean);
		}
		//GetAllList{input=; MoneyNum=0; XingNum=0; ByOneNum=410000; ByTwoNum=410100; classNum=1; zhutiId=0; dayId=0; Start=1; EndNum=10; }
		
		
//		{"id":33,"Sale_DataCName":"昆明、大理、丽江、品质七日游 ","Sale_DataLinkName":"lxs51888","Sale_DataCClNum":4,
//			"Sale_DataCPath":"1,4","Sale_DataSmallImage":"120229035306_small.jpg","Sale_DataImage":"120229035306.jpg","Sale_DataOne":76.0000,
//			"Sale_DataTwo":1680.0000,"Sale_DataThr":1680.0000,"Sale_DataFour":76.0000,"Sale_DataMarkTime":"\/Date(1333337956203)\/",
//			"Sale_DataRef":true,"Sale_DataClickNum":536,"Sale_DataIsPay":0,"Sale_DataSmallAbout":"散客天天发，需要提前6天报名",
//			"Sale_DataAbout":"\u003cP\u003e★特别服务：鲜花接团、每天一瓶矿泉水\u003cBR\u003e★特别餐饮：行程中赠送丽江纳西喜宴，大理砂锅鱼，" +
//		}
		
		envelope.bodyOut = object; //
		envelope.dotNet = true; 
		try {
			httpTransportSE.call(actionUrl + methodName, envelope); 
			SoapObject result=(SoapObject)envelope.bodyIn;
			setJsonArray(result,baseBean,methodName + "Result");
			return 0;
		} catch (Exception e) { 
		// TODO: handle exception 
			e.getStackTrace();
		} 
		return -1;
	}
	
	public JSONArray getJsonArray() {
		return jsonArray;
	}
	
	public void setJsonArray(SoapObject soapObject,LvYouRequestParmeterBaseBean bean,String property) {
		String array = "";
//		if (bean == null) {
//			array = (String) soapObject.getProperty("GetMapOneListResult").toString();
//		} else if (bean instanceof LvYouCityRequestParmeterBean) {
//			array = (String) soapObject.getProperty("GetMapTwoListResult").toString();
//		}
		
		array = (String) soapObject.getProperty(property).toString();
		
		try {
			JSONArray jsonArray = new JSONArray(array);
			this.jsonArray = jsonArray;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	
}











